//app.js  
App({
  onLaunch: function () {
    //调用API从本地缓存中获取数据  
    // var logs = wx.getStorageSync('logs') || []  
    // logs.unshift(Date.now())  
    // wx.setStorageSync('logs', logs) 
    wx.login({
      success: function (res) {
        if (res.code) {
          //发起网络请求  
        } else {
          console.log('获取用户登录态失败！' + res.errMsg)
        }
      }
    });   
  },
  onShow : function(){

  },
  onHide : function() {

  },
  onError : function() {

  },
  getUserInfo: function (cb) {
    var that = this;
    if (this.globalData.userInfo) {
      typeof cb == "function" && cb(this.globalData.userInfo)
    } else {
      //调用登录接口  
      wx.login({
        success: function () {
          wx.getUserInfo({
            success: function (res) {
              that.globalData.userInfo = res.userInfo;
              typeof cb == "function" && cb(that.globalData.userInfo)
            }
          })
        }
      });
    }
  },
  globalData: {
    userInfo: null,
    personageAddressList: new Array()
  },
  // 储存 修改 昵称
  newUserInfo: function() {
    nameBack :'';
    var that = this;
    if (this.globalData.userInfo) {
      typeof cb == "function" && cb(this.globalData.userInfo)
    } else {
      //调用登录接口  
      wx.getStorage({
        key: 'nickName',
        success: function(res) {
          console.log('res.data',res.data);
          nameBack: res.data;
          console.log('nameBack',nameBack);
        },
      })
    }
  },
  // 验证手机号 格式
  telephone: function(mobile) {
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/;
    if (mobile.length == 0) {
      wx.showToast({
        title: '请输入手机号码！',
        icon: 'loading',
        duration: 2000
      })
      return false;
    }else if (mobile.length != 11) {
      wx.showToast({
        title: '手机号格式错误！',
        icon: 'loading',
        duration: 2000
      })
      return false;
    }else if (!myreg.test(mobile)) {
      wx.showToast({
        title: '手机号格式错误！',
        icon: 'loading',
        duration: 2000
      })
      return false;
    } else {
      console.log('手机号填写正确');
      return true;
    }
  },
  // 储存 修改 手机号
  getNewTel: function(newTel) {
    wx.getStorage({
      key: 'telPhone',
      success: function(res) {
        console.log('得到 新手机号',res.data)
        newTel:res.data
      },
    })
  },
  countdown: function(that) {
    var second = that.data.second
    if (second == 0) {
      // console.log("Time Out...");
      that.setData({
        second: "Time Out..."
      });
      return;
    }
    var time = setTimeout(function () {
      that.setData({
        second: second - 1
      });
      countdown(that);
    }, 1000)
  },
  smalltoBIG: function(n) {    
    var fraction = ['角', '分'];    
    var digit = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'];    
    var unit = [['元', '万', '亿'], ['', '拾', '佰', '仟']];    
    var head = n < 0 ? '负' : '';    
    n = Math.abs(n);    
              
    var s = '';    
              
    for(var i = 0; i<fraction.length; i++)  {    
      s += (digit[Math.floor(n * 10 * Math.pow(10, i)) % 10] + fraction[i]).replace(/零./, '');
    }
    s = s || '整';
    n = Math.floor(n);

    for (var i = 0; i < unit[0].length && n > 0; i++) {
      var p = '';
      for (var j = 0; j < unit[1].length && n > 0; j++) {
        p = digit[n % 10] + unit[1][j] + p;
        n = Math.floor(n / 10);
      }
      s = p.replace(/(零.)*零$/, '').replace(/^$/, '零') + unit[0][i] + s;
    }
    return head + s.replace(/(零.)*零元/, '元').replace(/(零.)+/g, '零').replace(/^整$/, '零元整');    
  }  
})  